import axios from 'axios'
import NProgress from 'nprogress'
import until from '../until'
import { Message } from 'element-ui'

const request=axios.create({
    baseURL:process.env.VUE_APP_BASE_URL
})

request.interceptors.request.use(
    function(config){
        NProgress.start()
        if(config.url!="login"){
            config.headers.Authorization=until.getToken();                         
        }
        return config
    },
    function(error){
        NProgress.done();
        Message.error("请求失败，请稍后再试");
        return Promise.reject(error);
    }
)

request.interceptors.response.use(
    function(response){
        let result= response.data
        if(result.meta.status>=400){
            Message.error(result.meta.msg);
            let err=new Error(result.meta.msg)
            err.response=response;
            return Promise.reject(err);
        }

        return result.data;
    },
    function(error){
        return Promise.reject(error)
    }
)

export default request;
